﻿using System;
using System.IO;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Web;

namespace BaseData.UI
{
    public class BasePage : System.Web.UI.Page
    {
        /// <summary>
        /// 获取Request字符串值
        /// </summary>
        /// <param name="key">Request字符串变量名</param>
        /// <returns>字符串值</returns>
        public static string GetRequest(string key)
        {
            return GetRequest(key, true, false);
        }

        /// <summary>
        /// 获取Request字符串值
        /// </summary>
        /// <param name="key">Request字符串变量名</param>
        /// <param name="isKeyword">是否过滤敏感词</param>
        /// <param name="isAll">暂时未用</param>
        /// <returns>过滤后的字符串</returns>
        public static string GetRequest(string key, bool isKeyword, bool isAll)
        {
            System.Web.HttpRequest Request = System.Web.HttpContext.Current.Request;
            if (Request.Form[key] == null && Request.QueryString[key] == null)
            {
                return string.Empty;
            }
            string returnstr = string.Empty;
            if (Request.Form[key] != null)
            {
                if (Request.Form[key].ToString().Trim().Length > 0)
                {
                    returnstr = ReplaceChars(Request.Form[key].ToString().Trim());
                }
            }
            else if (Request.QueryString[key] != null)
            {
                if (Request.QueryString[key].ToString().Trim().Length > 0)
                {
                    returnstr = ReplaceChars(Request.QueryString[key].ToString().Trim());
                }
            }
            return returnstr;
        }

        /// <summary>
        /// 接受Request字符串并截取相应长度
        /// </summary>
        /// <param name="keystr">Request字符串</param>
        /// <param name="length">截取长度</param>
        /// <returns>过滤后的字符串</returns>
        public static string GetRequest(string keystr, int length)
        {
            string returnstr = GetRequest(keystr);
            if (returnstr.Length > length)
            {
                returnstr = returnstr.Substring(0, length);
            }
            return returnstr;
        }

        /// <summary>
        /// 过滤SQL关键字
        /// </summary>
        /// <param name="oraString">原字符串</param>
        /// <returns>过滤后的字符串</returns>
        public static string ReplaceChars(string oraString)
        {
            if (oraString == null || oraString == "" || oraString.Trim() == "") return "";
            string[] sqlBadChar = { "script", "exec", "select", "update", "delete", "declare", "'", "&" };
            string[] sqlNewChar = { "ＳＣＲＩＰＴ", "ｅｘｅｃ", "ｓｅｌｅｃｔ", "ｕｐｄａｔｅ", "ｄｅｌｅｔｅ", "ｄｅｃｌａｒｅ", "\"", "" };
            for (int i = 0; i < sqlBadChar.Length; i++)
            {
                oraString = oraString.Replace(sqlBadChar[i], sqlNewChar[i]);
            }

            return oraString;
        }

        /// <summary>
        /// 过滤特殊符号
        /// </summary>
        /// <param name="oraString">原字符串</param>
        /// <returns>过滤后的字符串</returns>
        public static string ReplaceSpecialChar(string oraString)
        {
            if (string.IsNullOrEmpty(oraString) || oraString.Trim().Length == 0)
            {
                return string.Empty;
            }
            string[] specialChars = { "~", "!", "@", "#", "$", "%", "^", "&", "*", "，", "。", "、", "；", "：", "？", "…", "‘", "“", "《", "｛", "〖", "【", "】", "〗", "｝", "》", "”", "’", "＃", "～", "?", "※", "＋", "－", "×", "÷", "№", "＄", "￥", "§", "‰", "—", "§", "№", "☆", "★", "○", "●", "◎", "◇", "◆", "□", "℃", "‰", "€", "■", "△", "▲", "※", "→", "←", "↑", "↓", "〓", "¤", "°", "＃", "＆", "＠", "＼", "︿", "＿", "￣", "―", "♂", "♀", "￠", "￡", "α", "β", "γ", "δ", "ε", "ζ", "η", "θ", "ι", "κ", "λ", "μ", "ν", "ξ", "ο", "π", "ρ", "σ", "τ", "υ", "φ", "χ", "ψ", "ω", "！", "％", "…", "…", "＊" };
            foreach (string s in specialChars)
            {
                oraString = oraString.Replace(s, "");
            }
            return oraString;
        }

        /// <summary>
        /// 获取配置文件值
        /// </summary>
        /// <param name="key">变量名</param>
        /// <returns>配置文件值</returns>
        public static string GetConfig(string key)
        {
            return System.Configuration.ConfigurationManager.AppSettings[key];
        }

        /// <summary>
        /// 获取客户端IP地址
        /// </summary>
        /// <returns>IP地址</returns>
        public static string GetClientIP()
        {
            string ipAddress = string.Empty;
            if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] == null)
            {
                ipAddress = System.Web.HttpContext.Current.Request.ServerVariables["Remote_Addr"];
            }
            else
            {
                ipAddress = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            }
            if (ipAddress.IndexOf(",") > 0)
            {
                ipAddress = ipAddress.Substring(0, ipAddress.IndexOf(","));
            }
            return ipAddress;
        }

        /// <summary>
        /// 输出Excel报表
        /// </summary>
        /// <param name="dtDataSourse">报表数据源</param>
        /// <param name="fileName">输出文件名</param>
        /// <param name="titles">Excel表头标题名</param>
        /// <param name="fields">Excel绑定字段名</param>
        public static void ToExcel(DataTable dtDataSourse, string fileName, string[] titles, string[] fields)
        {
            //判断绑定字段
            if (fields == null || fields.Length == 0)
            {
                throw new ArgumentNullException("绑定标题为空");
            }
            if (titles == null || titles.Length != fields.Length)
            {
                throw new ArgumentException("绑定字段为空或者是标题和字段长度不等", "fields");
            }

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            fileName = fileName + ".xls";
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            //输出Excel表头
            foreach (string title in titles)
            {
                HttpContext.Current.Response.Write(title + "\t");
            }
            HttpContext.Current.Response.Write("\n");
            //输出Excel内容 
            foreach (DataRow dr in dtDataSourse.Rows)
            {
                foreach (string field in fields)
                {
                    HttpContext.Current.Response.Write(dr[field].ToString() + "\t");
                }
                HttpContext.Current.Response.Write("\n");
            }
            HttpContext.Current.Response.End();
        }
    }
}